package com.demo.shardingsphere.user.config;


import com.demo.shardingsphere.util.SpringContextUtils;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.sql.SQLException;
import java.util.Collection;

/**
 * 表路由
 */
public class UserTableAlgorithm implements PreciseShardingAlgorithm<Long> {


    /**
     * 用户id分表
     */
    @Override
    public String doSharding(Collection<String> availableTargetNames,
                             PreciseShardingValue<Long> shardingValue) {
        Long value = shardingValue.getValue();
        String tableName= null;
        try {
            //表名字
            tableName = SpringContextUtils.getShardingService().getTable(value);
        } catch (SQLException e) {
            throw  new RuntimeException("tableName error",e);
        }
        return tableName;
    }
}